//
//  FVPersenViewController.m
//  MinShengBank
//
//  Created by mac on 13-11-11.
//  Copyright (c) 2013年 Hq. All rights reserved.
//

#import "FVPersenViewController.h"

@interface FVPersenViewController ()
{
    NSInteger index;
}
@end

@implementation FVPersenViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        index = 0;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
    
    UITableView *mytableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width-10, self.view.height) style:UITableViewStyleGrouped];
    mytableview.dataSource = self;
    mytableview.backgroundColor = [UIColor clearColor];
    mytableview.backgroundView = nil;
    mytableview.delegate = self;
    mytableview.scrollEnabled = NO;
    mytableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:mytableview];
    
       self.title = @"个人信息";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 0, 30, 30);
    [button setImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    UIButton *xiugai = [UIButton buttonWithType:UIButtonTypeCustom];
    xiugai.frame = CGRectMake(10, 0, 30, 30);
    [xiugai setImage:[UIImage imageNamed:@"写评论.png"] forState:UIControlStateNormal];
    [xiugai addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:xiugai];
}

#pragma mark 返回
-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)edit
{
    UIButton *xiugai = [UIButton buttonWithType:UIButtonTypeCustom];
    xiugai.frame = CGRectMake(10, 0, 30, 30);
    [xiugai setImage:[UIImage imageNamed:@"发送.png"] forState:UIControlStateNormal];
    [xiugai addTarget:self action:@selector(fasong) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:xiugai];
    
    adder.enabled = YES;
    [adder becomeFirstResponder];
    tel.enabled = YES;
    email.enabled = YES;
}

-(void)fasong
{
    NSString *tell = adder.text;
    NSString *phone = tel.text;
    NSString *ema = email.text;
     NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:ema,@"email",phone, @"mobile",tell, @"phone",[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"],@"userid",nil];
    
    [[RequestServer instance] doActionAsync:0 withAction:@"LoginAction$changeInfo" withParameters:para withDelegate:self];
}

- (void)requestFinished:(NSNumber *)code withMessage:(NSString *)message withData:(id)data
{
    NSLog(@"%d",[code intValue]);
    if (code == 0) {
        NSString *tell = adder.text;
        NSString *phone = tel.text;
        NSString *ema = email.text;
        
        ShowMessage(@"修改成功");
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:phone forKey:@"mobile"];
        [defaults setObject:tell forKey:@"phone"];
        [defaults setObject:ema forKey:@"email"];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else if ([code intValue]== 1) {
        ShowMessage(@"服务器异常");
    }else{
        ShowMessage(@"上传头像失败");
    }
   
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier ;
    
    CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (indexPath.row == 0) {
        UILabel* title = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 70, 30)];
        title.backgroundColor = [UIColor clearColor];
        title.textAlignment = NSTextAlignmentLeft;
        title.text = @"登录名:";
        title.font = [UIFont fontWithName:@"MicrosoftYaHei" size:17.0];
        [cell addSubview:title];
        
        addTime = [[UILabel alloc]initWithFrame:CGRectMake(85,10 , 200, 30)];
        addTime.backgroundColor = [UIColor clearColor];
        addTime.textAlignment = NSTextAlignmentLeft;
        addTime.font = [UIFont fontWithName:@"MicrosoftYaHei" size:17.0];
        addTime.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginname"];
        [cell addSubview:addTime];
        
    }else  if (indexPath.row == 1) {
        UILabel* title = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 70, 30)];
        title.backgroundColor = [UIColor clearColor];
        title.textAlignment = NSTextAlignmentLeft;
        title.text = @"电   话:";
        title.font = [UIFont fontWithName:@"MicrosoftYaHei" size:17.0];
        [cell addSubview:title];
        
        adder = [[UITextField alloc]initWithFrame:CGRectMake(85,10 , 200, 30)];
        adder.backgroundColor = [UIColor clearColor];
        adder.textAlignment = NSTextAlignmentLeft;
        adder.enabled = NO;
        adder.font = [UIFont fontWithName:@"MicrosoftYaHei" size:17.0];
        adder.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"phone"];
        [cell addSubview:adder];
        
    }else if (indexPath.row == 2) {
        UILabel* title = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 70, 30)];
        title.backgroundColor = [UIColor clearColor];
        title.textAlignment = NSTextAlignmentLeft;
        title.text = @"手   机:";
        title.font = [UIFont fontWithName:@"MicrosoftYaHei" size:17.0];
        [cell addSubview:title];
        
        tel = [[UITextField alloc]initWithFrame:CGRectMake(85,10 , 200, 30)];
        tel.backgroundColor = [UIColor clearColor];
        tel.textAlignment = NSTextAlignmentLeft;
        tel.font = [UIFont fontWithName:@"MicrosoftYaHei" size:17.0];
        tel.enabled = NO;
        tel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"mobile"];
        [cell addSubview:tel];
    }else if (indexPath.row == 3) {
        UILabel* title = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 70, 30)];
        title.backgroundColor = [UIColor clearColor];
        title.textAlignment = NSTextAlignmentLeft;
        title.text = @"邮   箱:";
        title.font = [UIFont fontWithName:@"MicrosoftYaHei" size:17.0];
        [cell addSubview:title];
        
        email = [[UITextField alloc]initWithFrame:CGRectMake(85,10 , 200, 30)];
        email.backgroundColor = [UIColor clearColor];
        email.textAlignment = NSTextAlignmentLeft;
        email.enabled = NO;
        email.font = [UIFont fontWithName:@"MicrosoftYaHei" size:17.0];
        email.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"];
        [cell addSubview:email];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
